package com.train.batch.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.train.batch.resp.JobPageResp;
import com.train.batch.dto.AddJobDTO;
import com.train.batch.dto.JobPageDTO;
import com.train.batch.mapper.QrtzJobRecordMapper;
import com.train.batch.po.QrtzJobRecordPO;
import com.train.batch.service.QrtzJobRecordService;
import com.train.common.enums.impl.BizExceptionEnums;
import com.train.common.exception.GlobalException;
import com.train.common.resp.PageResp;
import com.train.common.utils.date.LocalDateTimeUtils;
import com.train.quartz.constants.ParamMapConstants;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class QrtzJobRecordServiceImpl extends ServiceImpl<QrtzJobRecordMapper, QrtzJobRecordPO> implements QrtzJobRecordService {

    @Autowired
    private QrtzJobRecordMapper qrtzJobRecordMapper;

    @Autowired
    private Scheduler scheduler;

    @Override
    public void saveJob(String jobName, String groupName, String cronExpress) throws SchedulerException {
        JobDetail jobDetail = scheduler.getJobDetail(new JobKey(jobName, groupName));
        Trigger.TriggerState triggerState = scheduler.getTriggerState(new TriggerKey(jobName, groupName));
        Trigger trigger = scheduler.getTrigger(new TriggerKey(jobName, groupName));
        AddJobDTO addJobDTO = new AddJobDTO();
        addJobDTO.setJobName(jobName);
        addJobDTO.setGroupName(groupName);
        addJobDTO.setRetryInterval(jobDetail.getJobDataMap().getString(ParamMapConstants.JOB_INTERVAL));
        addJobDTO.setRetryCount(jobDetail.getJobDataMap().getString(ParamMapConstants.RETRY_COUNT));
        addJobDTO.setJobParam(jobDetail.getJobDataMap().getString(ParamMapConstants.JOB_PARAM));
        addJobDTO.setCronExpress(cronExpress);
        addJobDTO.setNextFireTime(LocalDateTimeUtils.parse(trigger.getNextFireTime()));
        addJobDTO.setPreFireTime(LocalDateTimeUtils.parse(trigger.getPreviousFireTime()));
        addJobDTO.setJobStatus(String.valueOf(triggerState.ordinal()));
        boolean isSaved = qrtzJobRecordMapper.saveJob(addJobDTO);
        if (!isSaved) {
            throw new GlobalException(BizExceptionEnums.EXP_SAVE_JOB);
        }
    }

    @Override
    public void editJob(String jobName, String groupName, String cronExpress, Date nextTime) {
        AddJobDTO addJobDTO = new AddJobDTO();
        addJobDTO.setJobName(jobName);
        addJobDTO.setGroupName(groupName);
        addJobDTO.setCronExpress(cronExpress);
        if (nextTime != null) {
            addJobDTO.setNextFireTime(LocalDateTimeUtils.parse(nextTime));
        }
        boolean isEdit = qrtzJobRecordMapper.editJob(addJobDTO);
        if (!isEdit) {
            throw new GlobalException(BizExceptionEnums.EXP_EDIT_JOB);
        }
    }

    @Override
    public void editJobStatus(String jobName, String groupName) throws SchedulerException {
        Trigger.TriggerState triggerState = scheduler.getTriggerState(new TriggerKey(jobName, groupName));
        AddJobDTO addJobDTO = new AddJobDTO();
        addJobDTO.setJobName(jobName);
        addJobDTO.setGroupName(groupName);
        addJobDTO.setJobStatus(String.valueOf(triggerState.ordinal()));
        boolean isEdit = qrtzJobRecordMapper.editStatusJob(addJobDTO);
        if (!isEdit) {
            throw new GlobalException(BizExceptionEnums.EXP_EDIT_JOB);
        }
    }

    @Override
    public void editExecuteTime(String jobName, String groupName) throws SchedulerException {
        Trigger trigger = scheduler.getTrigger(new TriggerKey(jobName, groupName));
        AddJobDTO addJobDTO = new AddJobDTO();
        addJobDTO.setJobName(jobName);
        addJobDTO.setGroupName(groupName);
        addJobDTO.setNextFireTime(LocalDateTimeUtils.parse(trigger.getNextFireTime()));
        addJobDTO.setPreFireTime(LocalDateTimeUtils.parse(trigger.getPreviousFireTime()));
        boolean isEdit = qrtzJobRecordMapper.editExecuteTime(addJobDTO);
        if (!isEdit) {
            throw new GlobalException(BizExceptionEnums.EXP_EDIT_JOB);
        }
    }

    @Override
    public void deleteJob(String jobName, String groupName) {
        boolean isDelete = qrtzJobRecordMapper.deleteJob(jobName, groupName);
        if (!isDelete) {
            throw new GlobalException(BizExceptionEnums.EXP_DELETE_JOB);
        }
    }

    @Override
    public PageResp<JobPageResp> pageList(JobPageDTO pageDTO) {
        return qrtzJobRecordMapper.pageList(pageDTO);
    }
}
